# !/usr/bin/env python
# -*- coding:utf-8 -*-
import requests
import redis
import json
import hashlib

import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
from config import dmp
from config import db


class boroughMain(object):
    def __init__(self):
        self.redisClient = redis.StrictRedis(host=db.redis.get("host"), port=db.redis.get("port"), db=db.redis.get("db"), password=db.redis.get("auth"))
        pass

    def update(self, city):
        """
        跟新数据
        :param city:
        :return:
        """
        url = dmp.borough
        report = requests.get(url)

        content = report.content

        resutl = json.loads(content)
        datas = resutl.get('data')

        #redis

        boroughKey = "borough_%s" % (city)
        for data in datas:
            boroughData = {"name":data, "id":datas[data]['id']}
            self.redisClient.hset(boroughKey, self.md5(data), boroughData)

        return True


    def md5(self, str):
        m2 = hashlib.md5()  # 获取资源
        m2.update(str)  # 生成MD5
        result = m2.hexdigest()  # 获取MD5
        return result




if __name__ == "__main__":
    boroughMain().start()